const express = require('express')
const app = express()
app.listen(3000,function(){
console.log('3000端口开启了，可以访问: http://localhost:3000')
})
const fs = require('fs')
let F = fs.readFileSync('persons.json','utf8')
F = JSON.parse(F)
console.log(F)
const path = require('path')





app.use(function(req,res,next){
    let d = new Date()
    let year = d.getFullYear()
    let month = d.getMonth()+1
    let day =d.getDate()
    let h = d.getHours()
    let m = d.getMinutes()
    let s = d.getSeconds()

    

    req.time = `${year}年${month}月${day}日${h}时${m}分${m}秒`
    next()

})





//get就是你想要网页从当前页面跳转到另一个你想要的页面（我自己的理解）
app.get('/login',(req,res)=>{
    let fill = path.join(__dirname,'view/login.html')
    res.sendFile(fill)
}) 
app.get('/dologin',(req,res)=>{
    let D = req.query
    let ND = JSON.stringify(D)
    console.log(ND);

   let NF =  F.find(function(item){
        return item.username === D.username && item.password === D.password
})
console.log(NF)

if(NF){
    res.send('登陆成功'+ req.time)

}else{
    res.send('用户名或密码错误')
}
})
app.get('/register',(req,res)=>{
    let fill = path.join(__dirname,'view/register.html')
    res.sendFile(fill)
}) 
app.get('/doregister',(req,res)=>{
    let D = req.query
   let ND = JSON.stringify(D)
   let NF =  F.find(function(item){
    return item.username === D.username && item.password === D.password
})
    if( !NF){
        F.push(D)
        
        fs.writeFileSync('persons.json',JSON.stringify(F))
        res.send('注册成功'+ req.time)
        }else{
              res.send('用户已存在')
        }
        console.log(F)
})